# Maintainer: Jordan Irwin <antumdeluge@gmail.com>

_realname=liblcf
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.8.1
pkgrel=2
pkgdesc="Library to handle RPG Maker 2000/2003 and EasyRPG game data (mingw-w64)"
arch=("any")
mingw_arch=("mingw64" "ucrt64" "clang64" 'clangarm64')
url="https://easyrpg.org/"
msys2_repository_url="https://github.com/EasyRPG/liblcf"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libinih")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/EasyRPG/liblcf/archive/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('5ec89082d54f1a29d23fed54de70acab4375036d57828ff0fc7a88b81833d40f')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  "${MINGW_PREFIX}/bin/cmake.exe" --build ./

  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    "${extra_config[@]}" \
    ../${_realname}-${pkgver}

  "${MINGW_PREFIX}/bin/cmake.exe" --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}/bin/cmake.exe" --install ./

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}/bin/cmake.exe" --install ./

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
